package com.xuexiang.xuidemo.slice.progress;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import com.xuexiang.xui_lib.component.progress.loading.ARCLoadingView;
import com.xuexiang.xui_lib.component.progress.loading.LoadingDialog;
import com.xuexiang.xui_lib.component.progress.loading.LoadingViewLayout;
import com.xuexiang.xui_lib.component.progress.loading.MiniLoadingDialog;
import com.xuexiang.xui_lib.component.progress.loading.MiniLoadingView;
import com.xuexiang.xuidemo.ResourceTable;

import java.io.IOException;

public class ArcLoadingViewSlice extends AbilitySlice {
    private MiniLoadingView miniLoadingView;
    private ARCLoadingView mLoadingView;
    private LoadingViewLayout loadingViewLayout;
    private Button mBtSwitch, mBtLoadLayout, mBtLoadDialog, mBtnTestLoading;
    private Image image;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_arcloadingview);
        initView();
        setUpView();
    }

    public void initView() {
        mLoadingView = (ARCLoadingView) findComponentById(ResourceTable.Id_slice_arcloadingview_loading);
        mBtSwitch = (Button) findComponentById(ResourceTable.Id_slice_arcloadingview_start);
        mBtLoadLayout = (Button) findComponentById(ResourceTable.Id_slice_arcloadingview_loadinglayout);
        mBtLoadDialog = (Button) findComponentById(ResourceTable.Id_slice_arcloadingview_loadingDialog);
        mBtnTestLoading = (Button) findComponentById(ResourceTable.Id_slice_arcloadingview_testloadingDialog);
        miniLoadingView = (MiniLoadingView) findComponentById(ResourceTable.Id_slice_arcloadingview_MiniLoadingView);
        miniLoadingView.start();
        loadingViewLayout = (LoadingViewLayout) findComponentById(ResourceTable.Id_slice_arcloadingview_customlayout);
        image = (Image) findComponentById(ResourceTable.Id_slice_arcloadingview_backImage);
    }

    public void setUpView() {
        image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        mBtSwitch.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mLoadingView.isStart()) {
                    mBtSwitch.setText("开始");
                    mLoadingView.stop();
                } else {
                    mBtSwitch.setText("停止");
                    mLoadingView.start();
                }
            }
        });
        mBtLoadLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                loadingViewLayout.show(3000);
            }
        });
        mBtLoadDialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new LoadingDialog(getContext()).setDelayTime(3000).show();
            }
        });
        mBtnTestLoading.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    new MiniLoadingDialog(getContext()).setDelayTime(3000).show();
                } catch (NotExistException e) {
                    e.printStackTrace();
                } catch (WrongTypeException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        miniLoadingView.stop();
        if (mLoadingView.isStart()) {
            mLoadingView.stop();
        }

        if (loadingViewLayout.isLoading()) {
            loadingViewLayout.dismiss();
        }
    }
}
